#include "ScnsIic.c.h"

#if defined(SCNS_IIC_ENABLE)&&SCNS_IIC_ENABLE==1

#include "ScnsMath.h"

const ScnsBspIicConfig scnsBspIicConfig[SCNS_IIC_MAX]={
    {.tick=1000},
};

void scnsBspIicSetSdaDir(ScnsIicEnum iicN,uint8 in)
{
    switch(iicN)
    {
        case SCNS_IIC_SOFT_0:
        {
            const uint32 hb=scnsMathGetHighest1(CT_IIC_SDA_Pin);
            if(hb<8)
            {
                const uint32 of=hb*4;
                CT_IIC_SDA_GPIO_Port->CRL&=~((0XF)<<(of));
                CT_IIC_SDA_GPIO_Port->CRL|=((in?4:3)<<(of));
            }
            else
            {
                const uint32 of=(hb-8)*4;
                CT_IIC_SDA_GPIO_Port->CRH&=~((0XF)<<(of));
                CT_IIC_SDA_GPIO_Port->CRH|=((in?4:3)<<(of));
            }
            break;
        }
        case SCNS_IIC_SOFT_1:
        {
            const uint32 hb=scnsMathGetHighest1(IIC1_SDA_Pin);
            if(hb<8)
            {
                const uint32 of=hb*4;
                IIC1_SDA_GPIO_Port->CRL&=~((0XF)<<(of));
                IIC1_SDA_GPIO_Port->CRL|=((in?4:3)<<(of));
            }
            else
            {
                const uint32 of=(hb-8)*4;
                IIC1_SDA_GPIO_Port->CRH&=~((0XF)<<(of));
                IIC1_SDA_GPIO_Port->CRH|=((in?4:3)<<(of));
            }
            break;
        }
        default:;
    }
}

void scnsBspIicSetScl(ScnsIicEnum iicN,uint8 dat)
{
    switch(iicN)
    {
        case SCNS_IIC_SOFT_0:
        {
            CT_IIC_SCL_GPIO_Port->BSRR=(dat)?(CT_IIC_SCL_Pin):(CT_IIC_SCL_Pin<<16);
            break;
        }
        case SCNS_IIC_SOFT_1:
        {
            IIC1_SCL_GPIO_Port->BSRR=(dat)?(IIC1_SCL_Pin):(IIC1_SCL_Pin<<16);
            break;
        }
        default:;
    }
    
}

void scnsBspIicSetSda(ScnsIicEnum iicN,uint8 dat)
{
    switch(iicN)
    {
        case SCNS_IIC_SOFT_0:
        {
            CT_IIC_SDA_GPIO_Port->BSRR=(dat)?(CT_IIC_SDA_Pin):(CT_IIC_SDA_Pin<<16);
            break;
        }
        case SCNS_IIC_SOFT_1:
        {
            IIC1_SDA_GPIO_Port->BSRR=(dat)?(IIC1_SDA_Pin):(IIC1_SDA_Pin<<16);
            break;
        }
        default:;
    }
}

uint8 scnsBspIicGetSda(ScnsIicEnum iicN)
{
    switch(iicN)
    {
        case SCNS_IIC_SOFT_0: return (CT_IIC_SDA_GPIO_Port->IDR&CT_IIC_SDA_Pin)?1:0;
        case SCNS_IIC_SOFT_1: return (IIC1_SDA_GPIO_Port->IDR&IIC1_SDA_Pin)?1:0;
        default:;
    }
    return 1;
}

#endif
